<template>
  <div class="login-container">
    <!-- 登录导航栏 -->
    <van-nav-bar
      title="登录"
      class="page-nav-bar"
      left-arrow
      @click-left="$router.back()"
    >
      <template #left>
        <van-icon name="cross" size="21" />
      </template>
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        left-icon="smile-o"
        type="number"
        maxlength="11"
        :rules="userFormRules.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        maxlength="6"
        type="number"
        :rules="userFormRules.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="time"
            format="ss s"
            v-if="isCountDownShow === true"
            @finish="finish"
          />
          <van-button
            size="small"
            type="default"
            class="send-sms-btn"
            round
            @click="onSendSms"
            native-type="button"
            v-else
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button block type="info" native-type="submit" class="login-btn"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user.js';
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          {
            required: true,

            message: '请输入正确内容'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      time: 10 * 1000,
      isCountDownShow: false
    };
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onSubmit () {
      //  1.获取表单数据
      const user = this.user;
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      });
      try {
        const res = await login(user);

        this.$store.commit('setUser', res.data.data);
        this.$toast.success('登录成功！');
        this.$router.push('/my');
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或者验证码错误');
        } else {
          this.$toast.fail('登录失败');
        }
      }
      // 2.表单验证
      // 3.提交表单请求登录
      // 4.根据请求响应结果处理后续操作
    },
    async onSendSms () {
      // 1.校验手机号

      try {
        await this.$refs.loginForm.validate('mobile');
        this.$toast.success('验证通过！');
      } catch (err) {
        this.$toast.fail('验证失败！');
      }
      // 2.倒计时显示
      this.isCountDownShow = true;
      // 3.请求发送验证码
      try {
        await sendSms(this.user.mobile);
        this.$toast.success('发送验证码成功');
      } catch (error) {
        this.isCountDownShow = false;
        if (error.response.status === 429) {
          this.$toast.fail('手机号发送太频繁');
        } else {
          this.$toast.fail('手机号不正确');
        }
      }
    },
    finish () {
      this.isCountDownShow = false;
    }
  }
};
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 160px;
    height: 46px;
    line-height: 46px;
    background-color: #eeedef;
    font-size: 21px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
  }
  .login-btn {
    background-color: #67b0ff;
    border: none;
  }
}
</style>
